import pandas as pd
import numpy as np
from PIL import Image
import os
import json

# read num_to_class
json_path = './class_indices.json'
assert os.path.exists(json_path), "file: '{}' dose not exist.".format(json_path)
json_file = open(json_path, "r")
# {'0': 0, '1': 1, ...}  注意这里key 是字符串
num_to_class = json.load(json_file)
# {0: 0, 1: 1, ...}
class_to_num = dict((v, int(k)) for k, v in num_to_class.items())
# print(class_to_num)

save_csv = 'pred-csv-fu/tf_efficientnet_b4_ns/'
# save_csv = 'pred-csv/tf_efficientnetv2_s_in21ft1k/'
# save_csv = 'pred-csv/resnest50d/'
# save_csv = 'pred-csv/tf_efficientnet_b4_ns/'
# save_csv = 'pred-csv/ress50_effv2s_k10/'
# save_csv = 'pred-csv/ress50_effv2s_effb4_k10/'

df0 = pd.read_csv(f'{save_csv}pred-fold-0.csv')
df1 = pd.read_csv(f'{save_csv}pred-fold-1.csv')
df2 = pd.read_csv(f'{save_csv}pred-fold-2.csv')
df3 = pd.read_csv(f'{save_csv}pred-fold-3.csv')
df4 = pd.read_csv(f'{save_csv}pred-fold-4.csv')
# df5 = pd.read_csv(f'{save_csv}pred-fold-5.csv')
# df6 = pd.read_csv(f'{save_csv}pred-fold-6.csv')
# df7 = pd.read_csv(f'{save_csv}pred-fold-7.csv')
# df8 = pd.read_csv(f'{save_csv}pred-fold-8.csv')
# df9 = pd.read_csv(f'{save_csv}pred-fold-9.csv')

# 6个地方需要变动
list_num_label0 = []
for i in df0['category_id']:
    list_num_label0.append(class_to_num[i])
df0['num_label0'] = list_num_label0
# print(df0.head(3))

list_num_label1 = []
for i in df1['category_id']:
    list_num_label1.append(class_to_num[i])
df1['num_label1'] = list_num_label1
# print(df1.head(3))

list_num_label2 = []
for i in df2['category_id']:
    list_num_label2.append(class_to_num[i])
df2['num_label2'] = list_num_label2
# print(df2.head(3))

list_num_label3 = []
for i in df3['category_id']:
    list_num_label3.append(class_to_num[i])
df3['num_label3'] = list_num_label3
# print(df3.head(3))

list_num_label4 = []
for i in df4['category_id']:
    list_num_label4.append(class_to_num[i])
df4['num_label4'] = list_num_label4
# print(df4.head(3))

# list_num_label5 = []
# for i in df5['category_id']:
#     list_num_label5.append(class_to_num[i])
# df5['num_label5'] = list_num_label5
#
# list_num_label6 = []
# for i in df6['category_id']:
#     list_num_label6.append(class_to_num[i])
# df6['num_label6'] = list_num_label6
#
# list_num_label7 = []
# for i in df7['category_id']:
#     list_num_label7.append(class_to_num[i])
# df7['num_label7'] = list_num_label7

# list_num_label8 = []
# for i in df8['category_id']:
#     list_num_label8.append(class_to_num[i])
# df8['num_label8'] = list_num_label8
#
# list_num_label9 = []
# for i in df9['category_id']:
#     list_num_label9.append(class_to_num[i])
# df9['num_label9'] = list_num_label9

df_all = df0.copy()
df_all.drop(['category_id'], axis=1, inplace=True)
# print(df_all.head())
df_all['num_label1'] = list_num_label1
df_all['num_label2'] = list_num_label2
df_all['num_label3'] = list_num_label3
df_all['num_label4'] = list_num_label4
# df_all['num_label5'] = list_num_label5
# df_all['num_label6'] = list_num_label6
# df_all['num_label7'] = list_num_label7
# df_all['num_label8'] = list_num_label8
# df_all['num_label9'] = list_num_label9

# print(df_all.head(3))

df_all_transpose = df_all.copy().drop(['image_id'], axis=1).transpose()
# print(df_all_transpose.head())
df_mode = df_all_transpose.mode().transpose()  # 得到频次最多的数
# print(df_mode.sample(20))

voting_class = []
for each in df_mode[0]:
    voting_class.append(num_to_class[str(int(each))])
# print(voting_class)
df_all['category_id'] = voting_class
print(df_all.sample(20).iloc[1:])
df_final = df_all[['image_id', 'category_id']].copy()
# print(df_final.head(3))

df_final.to_csv(f'{save_csv}k10-vot-flip.csv', index=False)
